/**
 * 邯郸市政府采购网爬虫
 *  
 */
import {Base,Item} from '../base';
//const iconv = require('iconv-lite');
class hdzfcg extends Base{
    document:Document;
    constructor(){
        super();
        this.name = "邯郸市政府采购网";
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "ztb";
        this.collectionShowName = "招投标信息";
        this.keyField = "title";
    }
    getItems(pageIndex:number):Item[]{
        let num =pageIndex;
        let url=`http://www.zgazxxw.com/he-040008l772-${pageIndex}.html`;
        let result :Item[]= [];
        let content=this.spider.getSync(url);
        let $=this.cheerio.load(content);
        let that =this;
        let date=new Date();
        $('.list_zb table tr').each(function(index,ele){
           try {
            let item =that.buildItem();
            //  item.title=iconv.encode($(ele).find('a').text().trim().toString(),'UTF-8');
             item.title=$(ele).find('td').eq(0).find('a').attr('title').trim();  
             (<any>item)['采购日期']=date.getFullYear()+$(ele).find('td').eq(4).text();
              item.source=that.name;
              let uri=$(ele).find('td').eq(0).find('a').attr('href');
              (<any>item).url=$(ele).find('td').eq(0).find('a').attr('href');
              console.log(uri);
              result=that.getExtraItem(uri,item,result);
           } catch (error) {
               console.log(error);
           }
        });
        return result;
    }
    getExtraItem(url:string, item:Item, result:Item[]) {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content,{decodeEntities: false});
        (<any>item).content =(<any>$('.con_info').html())+(<any>$('.zhengwen').eq(1).html());
        result.push(item);
        return result;
    }
    getPageCount() {
        return 20;
    }
}
module.exports=new hdzfcg();